﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace App.Untiys
{
    public enum LogType
    {
        /// <summary>
        /// 基本信息
        /// </summary>
        Information,
        /// <summary>
        /// 错误日志
        /// </summary>
        Error,
        /// <summary>
        /// 错误日志
        /// </summary>
        RunError,
        /// <summary>
        /// 运行数据
        /// </summary>
        Waring,
        /// <summary>
        /// 程序运行信息
        /// </summary>
        Program,
        /// <summary>
        /// 采样运行信息
        /// </summary>
        Sampling,
        /// <summary>
        /// 上传
        /// </summary>
        Upload,
        /// <summary>
        /// 摄像头运行信息
        /// </summary>
        Camera,
             /// <summary>
             /// 获取计划
             /// </summary>
        Plan
    }

    /// <summary>
    /// 
    /// </summary>
    public enum LogSubitem
    {
        /// <summary>
        /// 系统相关
        /// </summary>
        System,
        /// <summary>
        /// 串口相关
        /// </summary>
        COM,
        /// <summary>
        /// PLC相关
        /// </summary>
        PLC,
        /// <summary>
        /// 日志相关
        /// </summary>
        Log,
        /// <summary>
        /// 命令相关
        /// </summary>
        Cmd,
        /// <summary>
        /// 接口相关
        /// </summary>
        Interface,
        /// <summary>
        /// SQL语句相关
        /// </summary>
        T_Sql,
        /// <summary>
        /// SQL语句相关
        /// </summary>
        Sqlserver,
        /// <summary>
        /// SQL语句相关
        /// </summary>
        Sqlite,
        /// <summary>
        /// 中间件
        /// </summary>
        DAS,
        /// <summary>
        /// 服务
        /// </summary>
        Service,
        /// <summary>
        /// 激光测距
        /// </summary>
        LDS50,
        /// <summary>
        /// 红外光幕
        /// </summary>
        XGA2880RS10,
        /// <summary>
        /// 窗体
        /// </summary>
        Forms
    }


    /// <summary>
    /// 日志类
    /// </summary>
    public class LogHelper
    {
        /// <summary>
        /// 清理日志期限
        /// </summary>
        private static int _ExpiredDays { set; get; }

        /// <summary>
        /// 日志清理线程
        /// </summary>
        private static Thread _thrdLog;

        /// <summary>
        /// 日志根目录
        /// </summary>
        private static string LogRoot
        {
            get
            {
                return System.Environment.CurrentDirectory.ToString() + "\\log";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="strMsg"></param>
        public static void DoWriteLogType(LogType type, string strMsg)
        {
            try
            {
                string strLogPath = LogRoot + "\\" + type.ToString();
                if (!(System.IO.Directory.Exists(strLogPath)))
                {
                    System.IO.Directory.CreateDirectory(strLogPath);
                }
                strLogPath = LogRoot + "\\" + type.ToString() + "\\" + DateTime.Now.ToString("yyyyMMdd-HH") + ".log";
                System.IO.TextWriter tw = new System.IO.StreamWriter(strLogPath, true);
                tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + strMsg);
                tw.Close();
            }
            catch (Exception ex)
            {
                DoWrite(ex.ToString());
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="Subitem"></param>
        /// <param name="strMsg"></param>
        public static void DoWriteLogTypeSubitem(LogType type, LogSubitem Subitem, string strMsg)
        {
            try
            {
                string strLogPath = LogRoot + "\\" + type.ToString() + "\\" + Subitem;
                if (!(System.IO.Directory.Exists(strLogPath)))
                {
                    System.IO.Directory.CreateDirectory(strLogPath);
                }
                strLogPath = LogRoot + "\\" + type.ToString() + "\\" + Subitem + "\\" + DateTime.Now.ToString("yyyyMMdd-HH") + ".log";
                System.IO.TextWriter tw = new System.IO.StreamWriter(strLogPath, true);
                tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + strMsg);
                tw.Close();
            }
            catch (Exception ex)
            {
                DoWrite(ex.ToString());
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="className"></param>
        /// <param name="strMsg"></param>
        public static void DoWriteLogTypeClass(LogType type, string className, string strMsg)
        {
            try
            {
                string strLogPath = LogRoot + "\\" + type.ToString() + "\\" + className;
                if (!(System.IO.Directory.Exists(strLogPath)))
                {
                    System.IO.Directory.CreateDirectory(strLogPath);
                }
                strLogPath = LogRoot + "\\" + type.ToString() + "\\" + className + "\\" + DateTime.Now.ToString("yyyyMMdd-HH") + ".log";
                System.IO.TextWriter tw = new System.IO.StreamWriter(strLogPath, true);
                tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + strMsg);
                tw.Close();
            }
            catch (Exception ex)
            {
                DoWrite(ex.ToString());
            }
        }

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="msg"></param>
        private static void DoWrite(string msg)
        {
            try
            {
                var strLogPath = LogRoot + "\\Logs";
                if (!(System.IO.Directory.Exists(strLogPath)))
                {
                    System.IO.Directory.CreateDirectory(strLogPath);
                }
                strLogPath = LogRoot + "\\Logs\\" + DateTime.Now.ToString("yyyyMMdd") + ".log";
                System.IO.TextWriter tw = new System.IO.StreamWriter(strLogPath, true);
                tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + msg);
                tw.Close();
            }
            catch (Exception ex)
            {
                DoWrite(ex.ToString());
            }
        }

        /// <summary>
        /// 启动日志清理线程
        /// </summary>
        /// <param name="day"></param>
        public static void DoClearSrart(int day)
        {
            _ExpiredDays = day;
            _thrdLog = new Thread(DoClear) { IsBackground = true };
            _thrdLog.Start();
        }

        /// 终止线程
        /// </summary>
        public static void DoClearStop()
        {
            if (_thrdLog != null)
                _thrdLog.Abort();
        }

        /// <summary>
        /// 删除过期日志
        /// </summary>
        private static void DoClear()
        {
            System.Threading.Thread.Sleep(15000);
            DirectoryInfo diTotal = new DirectoryInfo(LogRoot);
            ClearLog(diTotal);
        }

        /// <summary>
        /// 递归清理指定目录下过期日志
        /// </summary>
        /// <param name="diTotal"></param>
        private static void ClearLog(DirectoryInfo diTotal)
        {
            foreach (var _file in diTotal.GetFiles())
            {
                if (_file.CreationTime.AddDays(_ExpiredDays) < DateTime.Now)
                    _file.Delete();
            }

            if (diTotal.GetDirectories().Length > 0)
            {
                foreach (var _diChild in diTotal.GetDirectories())
                {
                    ClearLog(_diChild);
                }
            }
        }

    }
}